package sort.simple;

/**
 * 插入排序
 * @author caasi
 * @since 2022-01-04 11:37:50
 */
public class InsertionSort {

    public static void main(String[] args) {
        int n = 1000000;
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            int r = (int) (Math.random() * 1000);
            arr[i] = r;
        }
        //System.out.println(Arrays.toString(arr));
        long start = System.currentTimeMillis();
        InsertionSort.sort(arr);
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start) + "ms");
        //System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            for (int j = i; j > 0; j--) {
                if (arr[j] < arr[j - 1]) {
                    int temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                } else {
                    break;
                }
            }
        }
    }
}
